<?php

namespace App\Http\Controllers\Console;

use Validator;
use Illuminate\Http\Request;
use App\Model\Common\Project;
use App\Model\Common\UserLog;
use Illuminate\Support\Facades\DB;
use App\Model\Common\CategoryApi as Api;
use App\Model\Common\ProjectCategory as Category;

class ApiController extends CommonController
{
    protected $modelName = 'CategoryApi';
    protected $RULE = [
        'title' => 'required|max:60',
        'project_id' => 'required|numeric',
        'category_id' => 'required|numeric',
        'url' => 'required',
        'description' => 'required',
        'input_example' => 'required',
        'output_example' => 'required',
        'status' => 'required',
        'input_param.*' => 'required|max:60',
        'input_type.*' => 'required',
        'input_limited.*' => 'required',
        'input_required.*' => 'required',
        'input_description.*' => 'required',
        'output_param.*' => 'required|max:60',
        'output_type.*' => 'required',
        'output_description.*' => 'required',
    ];

    public function others()
    {
        $opt = ['user' => true];
        $Project = new Project();
        $projects = $Project->getListByParentKey(session('user:uid'), $opt);

        $Category = new Category();
        foreach ($projects as $k => $v) {
            $projectid = $v['id'];
            $projects[$k]['categorys'] = $Category->getListByParentKey($projectid, $opt);
        }

        $this->assign('projects', $projects);
    }

    public function add(Request $request)
    {
        if ('GET' == $request->method()) {
            $this->others();
            return $this->fetch();
        } else {
            $data = $request->post();
            $validator = Validator::make($data, $this->RULE);

            if ($validator->fails()) {
                return $this->output(10005, $validator->errors(), $data);
            }

            $Api = new Api();

            $re = $Api->add($data);

            if ($re) {
                if (method_exists($Api, 'refreshCache')) {
                    $Api->refreshCache($Api, 'insert');
                }
                UserLog::log($request, $Api);

                return $this->output(0, '新增成功', $Api->id);
            } else {
                return $this->output(10001, '新增失败'.$Api->getError(), null);
            }
        }
    }

    public function edit(Request $request)
    {
        $data = $request->post();
        $validator = Validator::make($data, $this->RULE);

        if ($validator->fails()) {
            return $this->output(10005, $validator->errors(), $data);
        }

        $m = new Api();

        $re = $m->edit($data);
        if (false !== $re) {
            if (method_exists($m, 'refreshCache')) {
                $m->refreshCache($request, 'update');
            }
            UserLog::log($request, $m);

            return $this->output(0, '更新成功', null);
        } else {
            return $this->output(10001, '更新失败'.$m->getError(), $data);
        }
    }
}
